﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace Sunfish.Zip
{
    ///   <summary>   
    ///   压缩解压类  
    ///   </summary>   
    public class Myzip
    {
        ZipOutputStream zos = null;
        string strBaseDir = "";
        
        public void dlZipDir(string srcDir,string desFineName)
        {
            zos = new ZipOutputStream(File.Create(desFineName));
            zos.UseZip64 = UseZip64.Off;
            zos.SetLevel(9);
            zos.Password = "";//设置压缩文件密码
            strBaseDir = srcDir + "\\";
            addZipEntry(strBaseDir);
            zos.Finish();
            zos.Close();
        }

        void addZipEntry(string PathStr)
        {

            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName);
            }
            foreach (FileInfo item in di.GetFiles())
            {
                FileStream fs = File.OpenRead(item.FullName);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string strEntryName = item.FullName.Replace(strBaseDir, "");
                ZipEntry entry = new ZipEntry(strEntryName);
                entry.Size = fs.Length;
                zos.PutNextEntry(entry);
                zos.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }

    }
}
